﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using PhoneDBModel;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["logout"] != null)
        {
            Session.Clear();
            Response.Cookies.Remove("name");
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtUser.Text == "Admin" && txtPassword.Text == ConfigurationManager.AppSettings["AdminPassword"])
        {
            Session["IsAdmin"] = true;
            Response.Redirect("/Admin");
        }

        PhoneDBEntities dbe = new PhoneDBEntities();
        User usr = dbe.Users.Where(u => u.Password == txtPassword.Text && u.UserName == txtUser.Text).FirstOrDefault();
        if (usr != null)
        {
            SessionHandler.AuthUser = usr;
            Session.Timeout = 100;
            
            Response.AppendCookie(new HttpCookie("name", AESCypher.encryptString(usr.UserName)));
            Response.AppendCookie(new HttpCookie("uid", AESCypher.encryptString(usr.Id.ToString())));

            Response.Redirect("Default.aspx");
        }
        else
        {
            lblMessage.Visible = true;
        }
    }
}